﻿#include "chatserver.h"
#include "clienthandle.h"
#include <QThreadPool>

ChatServer::ChatServer(QTcpServer *parent)
    : QTcpServer(parent)
{

}

ChatServer::~ChatServer()
{

}

// 启动服务器
void ChatServer::StartServer(int port)
{
    qDebug() << "服务器启动" << endl;
    listen(QHostAddress::Any, port); // QHostAddress::Any：绑定当前系统上的所有ip
}

// 处理客户端连接 自动触发 相当于accept函数
void ChatServer::incomingConnection(qintptr socketDescriptor)
{
    //创建新的ClientHandle对象，封装每个客户端的任务逻辑
    ClientHandle * c = new ClientHandle;
    //设置客户端的sock套接字
    c->SetSocket(socketDescriptor);
    // 将任务提交到线程池中执行
    QThreadPool::globalInstance()->start(c);
}
